package com.cloud.viewpostdemo

import android.animation.ObjectAnimator
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.doOnLayout
import com.cloud.viewpostdemo.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private val mainJob = SupervisorJob()
    private val uiScope = CoroutineScope(Dispatchers.Main + mainJob)
    private lateinit var binding: ActivityMainBinding
    private val TAG by lazy {
        "${javaClass.simpleName}@${System.identityHashCode(this)}"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
    }

    private fun initView() {
        //使用协程方式刷新
          uiScope.launch {
              binding.textview.text = "这是view使用协程刷新"
          }
        binding.textview.visibility = View.GONE
          //使用view.post刷新
          binding.textview.post {
              binding.textview.text = "这是view使用post刷新"
              Log.d(TAG,"view的内容${binding.textview.text} ${binding.textview.visibility}")
          }
          //使用handler.post刷新
          val handler = Handler(Looper.getMainLooper())
          handler.post {
              binding.textview.text = "这是view使用handler刷新"
          }
          //使用view.viewTreeObserver刷新
          binding.textview.viewTreeObserver.addOnGlobalLayoutListener {
              binding.textview.text = "这是view使用viewTreeObserver刷新"
          }
        binding.textview.doOnLayout {
            // 当布局确定后执行的代码
            binding.textview.text = "这是view使用doOnLayout刷新"
        }
    }
}